<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UploadImage
 *
 * @author WIN7
 */
class UploadImage {
//    private $sale_id = Yii::app()->session['saleId'];
    private function getExtention($filename) {
            $pos = strpos($filename,".");
            if (!$pos) return "";
            else {
            $length = strlen($filename) - $pos;
            $ext = substr($filename, $pos + 1, $length);
            return $ext;
    }      }  
            public function NewUpload() {
                        $list = array();
            $uploadir = 'files/';
            $validformat = array("jpg","png","bmp","gif","jpeg");
//            foreach ($_FILES['ItemForm']['name'] as $name => $value) {
//                $file = stripcslashes($_FILES['ItemForm']['name']);
//                $path = $_FILES['ItemForm']['name'][$name];
//                $new = explode('/', $path);
            $file =$_FILES['file']['name'];
                $ext = $this->getExtention($file);           
                $ext = strtolower($ext);                
//                $filename = substr($name, 0,strlen($name)-strlen($ext)-1);
//                var_dump($filename);
//                var_dump($ext);
                $size = filesize($_FILES['file']['tmp_name']);
//                echo var_dump($size);
//                echo var_dump($ext);
//                echo var_dump($file);
//                break;
                if (in_array($ext, $validformat)) {
                    if ($size < 2000*1024) {
                        $image = time().$file;
                        $newname = $uploadir.$image;
                        if (move_uploaded_file($_FILES['file']['tmp_name'], $newname)) {
//                                                           $list[] = $image;

                                return $image;
                        }
//                    $this->setlist_image($image);                   
                        else {
                            $this->addError('sub_image', "'{$file}': Error. Moving unsuccessful. Please select other image.");
//                            echo "Moving unsuccessful";
//                            exit();
                            return FALSE;
                        }
                    }
                    else {
                        $this->addError('sub_image', "'{$file}': Error. File too big. Please select other image.");
//                      echo "File too big";
//                      exit();
                return FALSE;
                    }
                } else {
                    $this->addError('sub_image',"'{$file}': Error. Unknown extension. Only allow jpg,png,bmp,gif or jpeg");
//                    echo "Unknown extension";
//                    exit();
            return FALSE;
                }                
                    }       
            public function upload_files($files, $allow_size = 3145728) {
        $total = count($files['name']);
        if ($total <= 0)
            return false;
        $uploaded_items = array();
        if ($total == 1) {
            $file_info = pathinfo($files['name']);
            $file_name = time().$files['name'];
            $upload_dir = 'files/';
            try {
                if ($files['size'] > $allow_size)
                    return false;

                move_uploaded_file($files['tmp_name'], $upload_dir . $file_name);
                $new_file = array('name' => $file_name, 'size' => $files['size'], 'type' => $files['type'], 'url' => $upload_dir . $file_name);
                array_push($uploaded_items, $new_file);
            } catch (Exception $er) {
                echo $er->getMessage();
            }
        } else {
            for ($i = 0; $i < $total; $i++) {
                $file_info = pathinfo($files['name'][$i]);
                $file_name = time(). $files['name'];
                $upload_dir ='files/';
                try {
                    $file = array('name' => $files['name'][$i], 'type' => $files['type'][$i], 'tmp_name' => $files['tmp_name'][$i], 'error' => $files['error'][$i], 'size' => $files['size'][$i]);
                    if ($file['size'] > $allow_size)
                        continue;

                    move_uploaded_file($files['tmp_name'][$i], $upload_dir . $file_name);
                    $new_file = array('name' => $file_name, 'size' => $files['size'][$i], 'type' => $files['type'][$i], 'url' => $upload_dir . $file_name);
                    array_push($uploaded_items, $new_file);
                } catch (Exception $er) {
                    echo $er->getMessage();
                }
            }
        }
        return $uploaded_items;
    }
    public function GetDatabase($rowid) {
        $rowid = $rowid-1;
        $sql = "SELECT *
                FROM os_item
                WHERE sale_id =".Yii::app()->session['saleId'];
        $command = Yii::app()->db->createCommand($sql);
//        $command->bindParam(":saleId", Yii::app()->session['saleId'], PDO::PARAM_INT);
//        $command->queryRow();
        $data = $command->query();
        $rows_data =array();
        while ($row = $data->read()) {
            $rows_data[] = $row;
        }
        return $rows_data[$rowid]['list_image'];
    }
    public function SaveImage($list_image,$rowid) {
        $rowid = $rowid-1;
        $sql = "SELECT *
                FROM os_item
                WHERE sale_id =".Yii::app()->session['saleId'];
        $command = Yii::app()->db->createCommand($sql);
//        $command->bindParam(":saleId", Yii::app()->session['saleId'], PDO::PARAM_INT);
//        $command->queryRow();
        $data = $command->query();
        $rows_data =array();
        while ($row = $data->read()) {
            $rows_data[] = $row;
        }
        $Id = $rows_data[$rowid]['Id'];
        try {
            $list_image = json_encode($list_image);
                                $query="UPDATE os_item as a 
                                    SET  a.list_image='{$list_image}' 
                                    WHERE a.Id=".$Id; 
                                Yii::app()->db->createCommand($query)->execute();
            return TRUE;
        }  catch (Exception $e) {
            echo $e;
            return FALSE;
        }
    }
    
                    }
                    

?>
